﻿using System.Windows;
using DSPL.Services;
using DSPL.Contracts;
using DSPL.Common;
using System.Data;
using DSPL.Services.Services;
using DSPL.Contracts.Common;
using DSPL.Contracts.ViewModel;
using DSPL.Contracts.Command;
using DSPL.Finac.Common;
using System;

namespace DSPL.Finac.ViewModel
{
    public class AdjustmentAdditionViewModel : BaseViewModel
    {
        private bool isReferenceNoFocused;
        private bool isLocationCodeFocused;
        private bool isCostCodeFocused;
        private bool isProductCodeFocused;

        #region Define properties


        public bool IsLocationFromCodeFocused
        {
            get { return isLocationCodeFocused; }
            set
            {
                isLocationCodeFocused = value;
                this.OnPropertyChanged("IsLocationFromCodeFocused");

            }
        }
        
        public bool IsCostCodeFocused
        {
            get { return isCostCodeFocused; }
            set
            {
                isCostCodeFocused = value;
                this.OnPropertyChanged("IsCostCodeFocused");

            }
        }


        public bool IsProductCodeFocused
        {
            get { return isProductCodeFocused; }
            set
            {
                isProductCodeFocused = value;
                this.OnPropertyChanged("IsProductCodeFocused");

            }
        }


        public bool IsReferenceNoFocused
        {
            get { return isReferenceNoFocused; }
            set
            {
                isReferenceNoFocused = value;
                this.OnPropertyChanged("IsReferenceNoFocused");
            }
        }


        #endregion


        #region Constructor

        public AdjustmentAdditionViewModel()
            : base()
        {
            MessageTitle = "Adjustment Addition";
        }

        #endregion
        /// <summary>
        /// Validate mandatory data before save
        /// </summary>
        /// <param name="obj"></param>
        protected override void PreDataValidateExecuted(object obj)
        {

            IsLocationFromCodeFocused = false;
            IsCostCodeFocused = false;
            IsReferenceNoFocused = false;

            var messageService = new MessageService();
            if (CurrentRow["RefNo"].ToString().Trim() == string.Empty)
            {
                messageService.ShowMessage(MessageText.ReferenceNoNotEmpty, MessageTitle, MessageType.InfoOK);
                IsReferenceNoFocused = true;
                this.IsValid = false;
                return;
            }


            if (CurrentRow["LocationPk"] == DBNull.Value || Convert.ToInt32(CurrentRow["LocationPk"]) == 0)
            {
                messageService.ShowMessage(MessageText.LocationNotEmpty, MessageTitle, MessageType.InfoOK);
                IsLocationFromCodeFocused = true;
                this.IsValid = false;
                return;
            }

            if (CurrentRow["CostPk"] == DBNull.Value || Convert.ToInt32(CurrentRow["CostPk"]) == 0)
            {
                messageService.ShowMessage(MessageText.CostCodeNotEmpty, MessageTitle, MessageType.InfoOK);
                IsCostCodeFocused = true;
                this.IsValid = false;
                return;
            }
            
            this.IsValid = true;
        }

        protected override void RefreshExternalDataObjectsExecueted(object obj)
        {
            base.RefreshExternalDataObjectsExecueted(obj);
            AddColumnsToChildTable();

        }

        private void AddColumnsToChildTable()
        {
            if (DtChildTransaction != null)
            {
                if (!DtChildTransaction.Columns.Contains("ItemCode"))
                {
                    DtChildTransaction.Columns.Add("ItemCode");
                }
                if (!DtChildTransaction.Columns.Contains("ItemName"))
                {
                    DtChildTransaction.Columns.Add("ItemName");
                }
                if (!DtChildTransaction.Columns.Contains("Qoh"))
                {
                    DtChildTransaction.Columns.Add("Qoh");
                }
            }
        }
    }
}


